package net.flyinggroup.actuator;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.Map;

@Component
@Endpoint(id = "server-time")
public class ServerTimeEndpoint {
    private Mono<Long> getNtpTimeOffset() {
        return Mono.just(12L);
    }

    @ReadOperation
    public Mono<Map<String, Object>> reportServerTime() {
        return getNtpTimeOffset()
                .map(offset -> {
                    Map<String, Object> rsp = new LinkedHashMap<>();
                    rsp.put("serverTime", Instant.now().toString());
                    rsp.put("ntpOffsetMillis", offset);
                    return rsp;
                });
    }
}
